/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbeans;

import entities.CompteBancaire;
import entities.OperationBancaire;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import session.GestionnaireDeCompteBancaire;

/**
 *
 * @author Gael
 */
@Named(value = "operationMBean")
@SessionScoped
public class OperationMBean implements Serializable {
    @EJB
    private GestionnaireDeCompteBancaire gc;
    private CompteBancaire compte;
    private int solde;
    private String proprietaire;
    private String libelle;
    
    
    
    public String getProprietaire() {
        return proprietaire;
    }

    public void setProprietaire(String proprietaire) {
        this.proprietaire = proprietaire;
    }

    
    public CompteBancaire getCompte() {
        return compte;
    }

    public void setCompte(CompteBancaire compte) {
        this.compte = compte;
    }

    public int getSolde() {
        return solde;
    }

    public void setSolde(int solde) {
        this.solde = solde;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }
    
            
    

    /**
     * Creates a new instance of OperationMBean
     */
    public OperationMBean() {
    }
    
    public String operation(){
        if(solde<0){
            
            if(compte.retirerArgent(solde*-1)){
                OperationBancaire ob = new OperationBancaire(libelle , solde);
                compte.addOperationBancaire(ob);
                gc.update(compte);
                return "index";
            }

            return "operation";
        }else{
            if(solde == 0){
                return "operation"; 
            }else{
                compte.ajouterArgent(solde);
                OperationBancaire ob = new OperationBancaire(libelle , solde);
                compte.addOperationBancaire(ob);
                gc.update(compte);
                return "index";
            }
        }
    }
    public String operationAE(){
        if(solde<0){
            compte = gc.getCompteByProprietaire(proprietaire);
            if(compte.retirerArgent(solde*-1)){
                OperationBancaire ob = new OperationBancaire(libelle , solde);
                compte.addOperationBancaire(ob);
                gc.update(compte);
                return "index";
            }

            return "operation";
        }else{
            if(solde == 0){
                return "operation"; 
            }else{
                compte.ajouterArgent(solde);
                OperationBancaire ob = new OperationBancaire(libelle , solde);
                compte.addOperationBancaire(ob);
                gc.update(compte);
                return "index";
            }
        }
    }
}
